#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
int dp[100],a[100],pos[100];
//dp[i]表示长度为i的序列中末尾元素最小值
int lis(int n)
{
    memset(dp,0,sizeof(dp));
    int len=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]>=dp[len]) dp[++len]=a[i],pos[i]=len;
        else{
            int p=upper_bound(dp+1,dp+1+len,a[i])-dp;
            pos[i]=p;
            dp[p]=a[i];
        }
    }
    return len;
}
void trace(int len,int n)
{
    stack<int> x;
    for(int i=n;i>=1&&len;i--)
    {
        if(pos[i]==len) {
            x.push(a[i]);
            len--;
        }
    }
    while(!x.empty()) cout<<x.top()<<" ",x.pop();
}
int main()
{
    int k;cin>>k;
    for(int l=1;l<=k;l++)
    {
        int n;cin>>n;
        for(int i=1;i<=n;i++)
        cin>>a[i];
        int len=lis(n);
        cout<<"length="<<len<<"  "<<"LIS:";
        trace(len,n);
        cout<<endl;
    }
}